import { provide_metadata } from "../constant";
import { KoaContext } from "@/types/index";
import { RouteParamTypes } from "./route-params";

export const extractLikeValue = (ctx: KoaContext, value: { paramtype: string; data: any }) => {
  const { paramtype, data } = value;
  switch (paramtype) {
    case RouteParamTypes.QUERY:
      return data ? ctx.query[data] : ctx.query;
    case RouteParamTypes.BODY:
      return data && ctx.request.body ? ctx.request.body[data] : ctx.request.body;
    case RouteParamTypes.PARAM:
      return data ? ctx.params[data] : ctx.params;
    case RouteParamTypes.HEADERS:
      return data ? ctx.get(data) : ctx.headers;
    case RouteParamTypes.SESSION:
      return data ? ctx.session[data] : ctx.session;
    case RouteParamTypes.REQUEST_IP:
      return ctx["ip"];
    default:
      return ctx;
  }
};

export function getProviderUUId(module: any) {
  const metaData = Reflect.getMetadata(provide_metadata, module) as any;
  if (metaData && metaData.uuid) {
    return metaData.uuid;
  }
}
